/**
* File Name: pthrd_attr_detach.c
* Auther: Jichang
* Environment: Fedora 7 server
* pthread_attr_detach function test.
*/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<pthread.h>

void *tfn(void *arg)
{
	printf("thread: pid=%d, tid=%lu\n", getpid(), pthread_self());

	return NULL;
}

int main(int argc, char *argv[])
{
	pthread_t tid;
	pthread_attr_t attr;

	//init.
	int ret = pthread_attr_init(&attr);
	if(ret != 0) {
		fprintf(stderr, "attr init errror: %s\n", strerror(ret));
	}

	//set state: detached.
	ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	if(ret != 0) {
		fprintf(stderr, "set_detach errror: %s\n", strerror(ret));
	}
	
	//create: set attr.
	ret = pthread_create(&tid, &attr, tfn, NULL);
	if(ret != 0) {
		fprintf(stderr, "create errror: %s\n", strerror(ret));
	}

	//destroy
	ret = pthread_attr_destroy(&attr);
	if(ret != 0) {
		fprintf(stderr, "destory errror: %s\n", strerror(ret));
	}
	//sleep(1);

	//judge state.bolocked reclaim.
	ret = pthread_join(tid, NULL);
	if(ret != 0) {
		fprintf(stderr, "pthread_join errror: %s\n", strerror(ret));
	}

	printf("main: pid=%d, tid=%lu\n", getpid(), pthread_self());

	pthread_exit((void*)0);	
}

